#!/bin/bash

# data_dir
datadir=/nexus-data

if [ ! -d ${datadir} ]; then
	UUID=$(mdata-get sdc:uuid)
	DDS=zones/${UUID}/data

	if zfs list ${DDS} 1>/dev/null 2>&1; then
		zfs create ${DDS}/nexusdata || true

		zfs set mountpoint=${datadir} ${DDS}/nexusdata

		# znapzend for backup
		znapzendzetup create --recursive --tsformat='%Y-%m-%d-%H%M%S' --donotask \
			SRC '7day=>8hour,30day=>1day,1year=>1week,10year=>1month' ${DDS}
		/usr/sbin/svcadm enable svc:/pkgsrc/znapzend:default
	else
		mkdir -p ${datadir}
	fi
fi

if [[ -e ${datadir}/etc ]]; then
  [[ -e /opt/sonatype-work ]] && rm -rf /opt/sonatype-work
else
  [[ -e /opt/sonatype-work ]] && mv /opt/sonatype-work/nexus3/* ${datadir}  
fi

# ensure nexus etc path
mkdir -p ${datadir}/etc

# ensure nexus.properties
[[ -f ${datadir}/etc/nexus.properties ]] || cp /opt/nexus/etc/nexus-default.properties ${datadir}/etc/nexus.properties